package cn.lutu.adapter.controller;

import cn.lutu.domain.service.OrderService;
import cn.lutu.protocol.vo.OrderListVO;
import cn.lutu.protocol.vo.OrderVO;
import cn.lutu.protocol.vo.adminVO.AdminOrderVO;
import cn.lutu.security.jwtFilter.CurrentPrincipal;
import cn.lutu.security.security.CustomUserDetails;
import cn.lutu.util.response.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @date 2023-06-03
 * @author luoyinhan
 * */
@Slf4j
@Api(tags = "订单模块")
@RestController
@RequestMapping("/v1/orders/")
public class OrderController {
    @Autowired
    OrderService orderService;


    @PostMapping("{id}/delete")
    @ApiOperation("删除订单")
    public ResultVO deleteOrder(@PathVariable Long id){
        log.debug("删除订单请求,参数为{}",id);
        orderService.deleteOrder(id);
        return ResultVO.ok();
    }

    @PostMapping("{id}/cancel")
    @ApiOperation("取消订单")
    public ResultVO cancelOrder(@PathVariable Long id){
        log.debug("取消订单请求,参数为{}",id);
        orderService.cancelOrder(id);
        return ResultVO.ok();
    }

    @PostMapping("{id}/refund")
    @ApiOperation("订单退款")
    public ResultVO refundOrder(@PathVariable Long id){
        log.debug("订单退款请求,参数为{}",id);
        orderService.refundOrder(id);
        return ResultVO.ok();
    }

    @GetMapping("{status}/orderList")
    @ApiOperation("用户所有订单")
    public ResultVO getListOrderByStatus(@PathVariable String status,  @AuthenticationPrincipal @ApiIgnore CurrentPrincipal c){
        log.debug("查看用户所有订单请求,参数为{}{}",status,c.getId());
        System.out.println(c);
        List<OrderVO> list = orderService.getListOrderByStatus(status,c.getId());
        return ResultVO.ok(list);
    }

    @GetMapping("/{categoryId}/search")
    @ApiOperation("用户所有订单")
    public ResultVO getListOrderByCategoryAndKeyword(@PathVariable Long categoryId,@RequestParam String keyword,  @AuthenticationPrincipal @ApiIgnore CurrentPrincipal c){
        log.debug("搜索用户订单请求,用户id为{}",c.getId());
        List<OrderVO> list = orderService.getListByCategoryAndKeyword(categoryId, keyword, c.getId());
        return ResultVO.ok(list);
    }

    @GetMapping("list")
    @ApiOperation("所有用户的订单")
    public ResultVO getOrderList(){
        log.debug("查看所有用户订单请求");
        List<OrderListVO> list = orderService.getAllOrderList();
        return ResultVO.ok(list);
    }


}
